<template>
  <div>
    <el-form ref="form" :model="form" label-width="100px" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" />
      </el-form-item>

      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" placeholder="请选择部门负责人">
          <!-- <el-option label="小王" value="shanghai" /> -->
          <el-option v-for="item in employees" :key="item.id" :label="item.username" :value="item.username" />
        </el-select>
      </el-form-item>

      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" type="textarea" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">确定</el-button>
        <el-button @click="closeFn">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { getEmployeeSimpleAPI, addEmployeerAPI } from '@/api/employees.js'
import { getDepartDetailAPI, updateDepartmentsAPI } from '@/api/departments'
export default {
  props: {
    id: {
      type: String,
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    },
    list: {
      type: Array,
      required: true
    }
  },
  data() {
    const codeValidator = (rule, val, callback) => {
      let codeList = this.list.map(item => item.code)
      if (this.isEdit) {
        codeList = this.list.filter(item => item.id !== this.id).map(item => item.code)
        console.log(codeList)
      }

      console.log(codeList)
      codeList.includes(val) ? callback({ message: val + '已被占用' }) : callback()
    }
    const nameValidator = (rule, val, callback) => {
      let nameList = this.list.filter(item => item.pid === this.id).map(item => item.name)
      if (this.isEdit) {
        const hId = this.list.find(item => item.id === this.id)
        nameList = this.list.filter(item => item.pid === hId.pid && item.id !== this.id).map(item => item.name)
      }

      nameList.includes(val) ? callback({ message: val + '已存在' }) : callback()
    }

    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { validator: nameValidator, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { validator: codeValidator, trigger: 'blur' }
        ],
        manager: [{ required: true, message: '部门负责人不能为空', trigger: 'blur' }],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: 'blur' }
        ]
      },
      employees: []
    }
  },

  created() {
    if (this.isEdit) {
      this.getDepartDetail()
    } else {
      this.getEmployeeSimple()
    }
  },
  methods: {
    closeFn() {
      this.$emit('close')
    },
    onSubmit() {
      this.$refs.form.validate(val => {
        if (val) {
          this.isEdit ? this.updateDepartments() : this.addEmployeer()
        }
      })
    },
    async getDepartDetail() {
      const res = await getDepartDetailAPI(this.id)
      this.form = res.data
      console.log(res)
    },
    async updateDepartments() {
      const res = await updateDepartmentsAPI(this.form)
      this.$emit('success')
      console.log(res)
    },
    async addEmployeer() {
      const res = await addEmployeerAPI({
        code: this.form.code,
        introduce: this.form.introduce,
        manager: this.form.manager,
        name: this.form.name,
        pid: this.id
      })
      console.log(res)
      this.$emit('success')
    },
    async getEmployeeSimple() {
      const res = await getEmployeeSimpleAPI()
      this.employees = res.data
      console.log(res)
    },
    hCancel() {
      console.log(123)
    }
  }
}
</script>
<style lang=""></style>
